/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package modeltotree.impl;

import BoardGame.Board;
import BoardGame.Field;
import BoardGame.Figure;

import MocaTree.File;
import MocaTree.MocaTreeFactory;
import MocaTree.Node;

import de.upb.tools.sdm.*;

import java.lang.reflect.InvocationTargetException;

import java.util.*;

import modeltotree.ModeltotreePackage;
import modeltotree.TransformModelToTree;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;

import org.eclipse.emf.ecore.impl.EObjectImpl;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Transform Model To Tree</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class TransformModelToTreeImpl extends EObjectImpl implements
		TransformModelToTree {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected TransformModelToTreeImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return ModeltotreePackage.Literals.TRANSFORM_MODEL_TO_TREE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public File transform(Board board) {
		boolean fujaba__Success = false;
		File file = null;
		Node boardNode = null;
		Iterator fujaba__IterBoardNodeToRow = null;
		Object _TmpObject = null;
		Node row = null;
		Node fieldNode = null;
		Figure curFigure = null;
		Node figureNode = null;
		Iterator fujaba__IterBoardToCurField = null;
		Field curField = null;

		// story pattern ActivityNode3
		try {
			fujaba__Success = false;

			// create object file
			file = MocaTreeFactory.eINSTANCE.createFile();
			// create object boardNode
			boardNode = MocaTreeFactory.eINSTANCE.createNode();
			// assign attribute file
			file.setName("board.txt");
			// assign attribute boardNode
			boardNode.setName("Board");
			// create link
			boardNode.setFile(file);

			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		// story pattern ActivityNode1
		try {
			fujaba__Success = false;

			// check object board is really bound
			JavaSDM.ensure(board != null);
			// iterate to-many link fields from board to curField
			fujaba__Success = false;

			fujaba__IterBoardToCurField = new ArrayList(board.getFields())
					.iterator();

			while (fujaba__IterBoardToCurField.hasNext()) {
				try {
					curField = (Field) fujaba__IterBoardToCurField.next();

					// check object curField is really bound
					JavaSDM.ensure(curField != null);
					// story pattern ActivityNode2
					try {
						fujaba__Success = false;

						// check object boardNode is really bound
						JavaSDM.ensure(boardNode != null);
						// check object curField is really bound
						JavaSDM.ensure(curField != null);
						// iterate to-many link children from boardNode to row
						fujaba__Success = false;

						fujaba__IterBoardNodeToRow = boardNode.getChildren()
								.iterator();

						while (!(fujaba__Success)
								&& fujaba__IterBoardNodeToRow.hasNext()) {
							try {
								_TmpObject = fujaba__IterBoardNodeToRow.next();

								// ensure correct type and really bound of object row
								JavaSDM.ensure(_TmpObject instanceof Node);
								row = (Node) _TmpObject;

								// check optional
								if (row != null) {
									// check isomorphic binding between objects row and boardNode
									JavaSDM.ensure(!row.equals(boardNode));

									// attribute condition
									JavaSDM.ensure(row.getIndex() == curField
											.getY());

								}
								fujaba__Success = true;
							} catch (JavaSDMException fujaba__InternalException) {
								fujaba__Success = false;
							}
						}
						if (!fujaba__Success) {
							fujaba__Success = true;
							row = null;
						}
						// create object row
						if (row == null) {
							row = MocaTreeFactory.eINSTANCE.createNode();
						}

						// create object fieldNode
						fieldNode = MocaTreeFactory.eINSTANCE.createNode();
						// check optional
						if (row != null) {
							// assign attribute row
							row.setIndex(curField.getY());
							// assign attribute row
							row.setName("Row");
						}// assign attribute fieldNode
						fieldNode.setName("Field");
						// check optional
						if (row != null) {
							// create link
							fieldNode.setParentNode(row);

							// create link
							boardNode.getChildren().add(row);

						}
						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					// story pattern ActivityNode4
					try {
						fujaba__Success = false;

						// check object curField is really bound
						JavaSDM.ensure(curField != null);
						// check object fieldNode is really bound
						JavaSDM.ensure(fieldNode != null);
						// bind object
						curFigure = curField.getFigure();

						// check object curFigure is really bound
						JavaSDM.ensure(curFigure != null);

						// create object figureNode
						figureNode = MocaTreeFactory.eINSTANCE.createNode();
						// assign attribute figureNode
						figureNode.setName(curFigure.eClass().getName()
								.substring(0, 1));
						// create link
						figureNode.setParentNode(fieldNode);

						fujaba__Success = true;
					} catch (JavaSDMException fujaba__InternalException) {
						fujaba__Success = false;
					}

					fujaba__Success = true;
				} catch (JavaSDMException fujaba__InternalException) {
					fujaba__Success = false;
				}
			}
			JavaSDM.ensure(fujaba__Success);
			fujaba__Success = true;
		} catch (JavaSDMException fujaba__InternalException) {
			fujaba__Success = false;
		}

		return file;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eInvoke(int operationID, EList<?> arguments)
			throws InvocationTargetException {
		switch (operationID) {
		case ModeltotreePackage.TRANSFORM_MODEL_TO_TREE___TRANSFORM__BOARD:
			return transform((Board) arguments.get(0));
		}
		return super.eInvoke(operationID, arguments);
	}

} //TransformModelToTreeImpl
